<?php
namespace backend\controllers;


use backend\models\Information;
use backend\models\InformationClass;
use common\components\BaseController;
use Yii;
use \common\helps\tools;
use \common\helps\code;


/**
 * InformationClass controller
 */
class InformationClassController extends BaseController
{

    public $modelClass = 'backend\models\InformationClass';
    public function init()
    {
//        $this->enableAuth=false;
        parent::init();
    }


    /**
     * 获取资讯分类列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetInformationClassList(){
        $request =$this->request;
        $model = new InformationClass();
        $params = $request->queryParams;
        $total_data = $model->getInformationClassList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','名称','状态'];
            foreach($total_data as &$value){
                if($value['status']>1){
                    $value['status'] ='停用';
                }else{
                    $value['status'] ='启用';
                }
                unset($value['info']);
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'资讯分类列表');
        }


        return code::success($data);

    }


    /**
     * 删除分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelInformationClass(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new InformationClass();
            $params = $request->bodyParams;
            if(empty($params['id'])){
                return code::error('','参数错误');
            }
            if(!empty($params['type']) && $params['type']==3){ //删除
                $res = $model->delInformationClass(intval(trim($params['id'])),intval(trim($params['type'])));
                if($res){
                    return code::success();
                }else{
                    return code::error('error');
                }
            }else{
                return code::error('','操作类型错误');
            }


        }
    }


    /**
     * 停启用分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateInformationClass(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new InformationClass();
            $params = $request->bodyParams;
            if(empty($params['id'])){
                return code::error('','参数错误');
            }
            if(!empty($params['type']) && ($params['type']==1 || $params['type']==2)){
                $res = $model->delInformationClass(intval(trim($params['id'])),intval(trim($params['type'])));
                if($res){
                    return code::success();
                }else{
                    return code::error('error');
                }
            }else{
                return code::error('','操作类型错误');
            }


        }
    }


    /**
     * 编辑资讯分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditInformationClass(){
        if($this->request->isPost) {
            $admin_model = new InformationClass();
            $params = $this->request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $admin_model->load($params,'');
                if(!$admin_model->validate()){
                    $error = array_values($admin_model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$admin_model->editInformationClass($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }

    /**
     * 新增资讯分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddInformationClass(){
        if($this->request->isPost) {
            $admin_model = new InformationClass();
            $params = $this->request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $admin_model->load($params,'');
                if(!$admin_model->validate()){
                    $error = array_values($admin_model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$admin_model->editInformationClass($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }


}
